<?php

class Model_Image extends Zend_Db_Table_Abstract {
	
	protected $_name = 'images';
	
	public function getList($filters = array(), $sort = null, $page = null, $rowCount = null)
    { 
		$select = $this->select ();
        if ($filters != null)
        {
            if (is_array ( $filters ))
            {
                foreach ( $filters as $key => $val )
                    $select->where ( $key, $val );
            }
            else
            {
                $select->where ( $filters );
            }
        }
        
        if ($page != null)
            $select->limitPage ( $page, $rowCount );
        
        if ($sort != null)
            $select->order ( $sort );
            
        return $this->fetchAll ( $select );
	}
	
	
	public function createProjectImage($productID, $imageLink)
	{
		// create a new row in the table
		$row = $this->createRow ();
		 
		// set the row data
		$row->product_id = $productID;
		$row->img_link = $imageLink;
		 
		// save the new row
		$row->save ();
		 
		// now fetch the id of the row you just created and return it
		$id = $row->product_id; //$this->_db->lastInsertId();
		return $id;
	}
	
	public function updateProductImage($imageID, $productID, $imageLink)
	{
		// find the row that matches the id
		$row = $this->find ( $imageID )->current ();
		if ($row)
		{
			// set the row data
			$row->product_id = $productID;
			if($imageLink) {
				$row->img_link = $imageLink;
			}
			
			// save the new row
			$row->save ();
			return true;
		}
		else
		{
			throw new Zend_Exception ( "Update function failed; could not find row!" );
		}
	}
	
	public function deleteProductImage($id)
	{
		// find the row that matches the id
		$row = $this->find ( $id )->current ();
		if ($row)
		{
			$row->delete ();
			return true;
		}
		else
		{
			throw new Zend_Exception ( "Delete function failed; could not find row!" );
		}
	}
}

?>